package org.dromara.crawler.mongo.mongoservice.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mongoplus.conditions.query.LambdaQueryChainWrapper;
import com.mongoplus.model.PageResult;
import com.mongoplus.service.impl.ServiceImpl;
import org.bson.Document;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.crawler.domain.bo.KnowledgeTargetBo;
import org.dromara.crawler.mongo.mongodomain.KnowledgeEvent;
import org.dromara.crawler.mongo.mongoservice.KnowledgeEventService;
import org.springframework.stereotype.Service;


import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class KnowledgeEventServiceImpl extends ServiceImpl<KnowledgeEvent> implements KnowledgeEventService {

    @Override
    public KnowledgeEvent queryEventById(String id) {
        return null;
    }

    @Override
    public Map<String, Long> getPredictedCategoryStats() {
        Map<String, Long> stats = new HashMap<>();
        List<String> categories = Arrays.asList("台海政治", "台海经济", "台海军事");
        for (String category : categories) {
            long count = this.lambdaQuery()
                .eq(KnowledgeEvent::getEventFirstLevel, category)
                .count();
            stats.put(category, count);
        }
        return stats;
    }

    @Override
    public List<KnowledgeEvent> getEventAll() {
        return this.list();
    }

    @Override
    public List<KnowledgeEvent> queryEvent(KnowledgeTargetBo knowledgeTargetBo) {
        // 构建查询条件
        LambdaQueryChainWrapper<KnowledgeEvent> wrapper = this.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotEmpty(knowledgeTargetBo.getEventFirstLevel()), KnowledgeEvent::getEventFirstLevel, knowledgeTargetBo.getEventFirstLevel());
        wrapper.eq(ObjectUtil.isNotEmpty(knowledgeTargetBo.getEventSecondLevel()), KnowledgeEvent::getEventSecondLevel, knowledgeTargetBo.getEventSecondLevel());
        wrapper.eq(ObjectUtil.isNotEmpty(knowledgeTargetBo.getEventName()), KnowledgeEvent::getEventName, knowledgeTargetBo.getEventName());
        // 执行查询
        return wrapper.list();
    }

    public Page<KnowledgeEvent> queryByEventPage(KnowledgeTargetBo knowledgeTargetBo, PageQuery pageQuery) {
        if (pageQuery == null) {
            pageQuery = new PageQuery();
        }
        // 设置默认值
        if (pageQuery.getPageNum() == null || pageQuery.getPageNum() <= 0) {
            pageQuery.setPageNum(1); // 默认第一页
        }
        if (pageQuery.getPageSize() == null || pageQuery.getPageSize() <= 0) {
            pageQuery.setPageSize(10); // 默认每页10条
        }

        // 构建查询条件
        LambdaQueryChainWrapper<KnowledgeEvent> wrapper = this.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotEmpty(knowledgeTargetBo.getEventFirstLevel()), KnowledgeEvent::getEventFirstLevel, knowledgeTargetBo.getEventFirstLevel());
        wrapper.eq(ObjectUtil.isNotEmpty(knowledgeTargetBo.getEventSecondLevel()), KnowledgeEvent::getEventSecondLevel, knowledgeTargetBo.getEventSecondLevel());
        wrapper.eq(ObjectUtil.isNotEmpty(knowledgeTargetBo.getEventName()), KnowledgeEvent::getEventName, knowledgeTargetBo.getEventName());

        // 执行分页查询
        PageResult<KnowledgeEvent> pageResult = this.page(wrapper, pageQuery.getPageNum(), pageQuery.getPageSize());
        List<KnowledgeEvent> records = pageResult.getContentData();
        records.stream().forEach(knowledgeEvent -> {
            Document structuredData = knowledgeEvent.getStructuredData();
            knowledgeEvent.setLocation(structuredData.getString("location"));
            knowledgeEvent.setTime(structuredData.getString("time"));
        });

        // 转换为MyBatis Plus的Page对象
        Page<KnowledgeEvent> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        page.setRecords(records);
        page.setTotal(pageResult.getTotalSize());

        return page;
    }

}
